package iuhao.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * http://www.cnblogs.com/webglcn/p/5265901.html
 */
@Api(tags = "AsyncTaskController", description = "多线程异步调用")
@RestController
@RequestMapping("/test")
public class AsyncTaskController {

    @Autowired
    private AsyncTask asyncTask;

    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    @ApiOperation(value = "多线程测试1")
    public String doTask1() throws InterruptedException {
        long currentTimeMillis = System.currentTimeMillis();
        Future<String> task1 = asyncTask.task1();
        Future<String> task2 = asyncTask.task2();
        Future<String> task3 = asyncTask.task3();
        String result = null;
        while (true) {
            if (task1.isDone() && task2.isDone() && task3.isDone()) {
                // 三个任务都调用完成，退出循环等待
                break;
            }
            Thread.sleep(3000);
        }
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("task任务总耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
        result = "task任务总耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms";
        return result;
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    @ApiOperation(value = "多线程测试2")
    public String doTask2() throws InterruptedException {

        long currentTimeMillis = System.currentTimeMillis();
        asyncTask.task1();
        asyncTask.task2();
        asyncTask.task3();
        long currentTimeMillis1 = System.currentTimeMillis();
        System.out.println("task任务总耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
        return "task任务总耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms";
    }


    @RequestMapping(value = "/test3", method = RequestMethod.GET)
    @ApiOperation(value = "多线程测试--使用线程池")
    public String doTask3() throws InterruptedException {
        //使用一个线程池
        // 创建线程池
        System.out.println("使用线程池");
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        long start = System.currentTimeMillis();
        long end=0;
        List<Integer> numberList = getNumberList();
        try {
            for (Integer number : numberList) {
                System.out.println("第"+number+"个数");
                Runnable task = () -> {
                    try {
                        asyncTask.task1(number);
                        asyncTask.task2(number);
                        asyncTask.task3(number);
                    } catch (Exception e) {
                        //
                    }
                };
                //执行线程任务
                executorService.execute(task);
            }
            //关闭线程连接
            executorService.shutdown();

            while(true){
                if(executorService.isTerminated()){
                    end = System.currentTimeMillis();
                    System.out.println("用时: " + (end - start) + "ms");
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "用时: " + (end - start) + "ms";
    }


    @RequestMapping(value = "/test4", method = RequestMethod.GET)
    @ApiOperation(value = "多线程测试--不使用线程池")
    public String doTask4() throws InterruptedException {
        System.out.println("不使用线程池");
        //不使用线程池
        long startTime = System.currentTimeMillis();
        List<Integer> numberList = getNumberList();
        try {
            for (Integer number : numberList) {
                try {
                    System.out.println("第"+number+"个数");
                    long currentTimeMillis = System.currentTimeMillis();
                    asyncTask.task1(number);
                    asyncTask.task2(number);
                    asyncTask.task3(number);
                    long currentTimeMillis1 = System.currentTimeMillis();
                    System.out.println("第"+number+"个数的task任务总耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
                } catch (Exception e) {
                    //
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("不使用线程池task任务总耗时:" + (endTime - startTime) + "ms");
        return "不使用线程池task任务总耗时:" + (endTime - startTime) + "ms";
    }

    private List<Integer> getNumberList() {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        return list;

    }


}
